package com.example.testapplication.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;

import com.example.testapplication.R;
import com.example.testapplication.RecyclerGridActivity;
import com.example.testapplication.bean.Goods;
import com.example.testapplication.bean.GoodsInfo;
import com.example.testapplication.widget.RecyclerExtras.OnItemClickListener;
import com.example.testapplication.widget.RecyclerExtras.OnItemLongClickListener;

import java.util.ArrayList;

public class RecyclerGridAdapter extends RecyclerView.Adapter<ViewHolder> implements OnItemLongClickListener, OnItemClickListener {
    private final static String TAG = "RecyclerGridAdapter";
    private Context mContext; // 声明一个上下文对象
    private ArrayList<Goods> mGoodsArray;

    public RecyclerGridAdapter(Context context,ArrayList<Goods> goodsArray){
        mContext = context;
        mGoodsArray = goodsArray;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 根据布局文件item_grid.xml 生成视图对象
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_grid_demo,parent,false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        ItemHolder holder1 = (ItemHolder) holder;
        holder1.grid_pic.setImageResource(mGoodsArray.get(position).pic_id);
        holder1.grid_title.setText(mGoodsArray.get(position).title);
        // 列表项的点击事件需要自己实现
        holder1.ll_grid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickListener != null){
                    mOnItemClickListener.onItemClick(view,position);
                }

            }
        });
        holder1.ll_grid.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(mOnItemLongClickListener != null){
                    mOnItemLongClickListener.onItemLongClick(view,position);
                }
                return true;
            }
        });


    }

    // 获取列表项的个数
    @Override
    public int getItemCount() {
        return mGoodsArray.size();
    }

    // 获取列表项的类型
    public int getItemViewType(int position) {
        return 0;
    }

    // 获取列表项的编号
    public long getItemId(int position) {
        return position;
    }


    // 定义列表项的视图持有者
    public class ItemHolder extends RecyclerView.ViewHolder{
        public LinearLayout ll_grid; // 声明列表项的线性布局
        public ImageView grid_pic; // 声明列表图标的图像视图
        public TextView grid_title; // 声明列表项标题的文本视图

        public ItemHolder(View view){
            super(view);
            ll_grid = view.findViewById(R.id.ll_grid);
            grid_pic = view.findViewById(R.id.grid_pic);
            grid_title = view.findViewById(R.id.grid_title);
        }
    }

    private OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    // 声明列表项的长按监听器对象
    private OnItemLongClickListener mOnItemLongClickListener;
    public void setOnItemLongClickListener(OnItemLongClickListener listener){
        this.mOnItemLongClickListener = listener;
    }

    // 处理列表项的点击事件
    public void onItemClick(View view,int position){
        String desc = String.format("您点击了第%d项，栏目名称是%s", position + 1,
                mGoodsArray.get(position).title);
        Toast.makeText(mContext, desc, Toast.LENGTH_SHORT).show();
    }

    // 处理列表项的长按事件
    public void onItemLongClick(View view, int position) {
        String desc = String.format("您长按了第%d项，栏目名称是%s", position + 1,
                mGoodsArray.get(position).title);
        Toast.makeText(mContext, desc, Toast.LENGTH_SHORT).show();
    }




}
